import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-13
 * Time:19:37
 */
class UdpServerTest{
    //服务器
    private DatagramSocket socket;
    public  UdpServerTest(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动！");
        while(true){
            //接收解析
            DatagramPacket requestPacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(requestPacket);
            //转化成字符串
            String request=new String(requestPacket.getData(),0, requestPacket.getLength());
            //进行计算响应
            String response=process(request);
            //返回给客户端
            DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length
                    ,requestPacket.getSocketAddress());
            socket.send(responsePacket);
            //日志记录一下
            System.out.println(requestPacket.getAddress().toString()+" "+requestPacket.getPort()+" "+request+" "+response);
        }
    }

    private String process(String request) {
        return request;
    }
}

public class UdpServer {
    public static void main(String[] args) throws IOException {
        UdpServerTest serverTest=new UdpServerTest(9090);
        serverTest.start();
    }
}
